package com.book.controller.admin;

import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.book.common.BaseController;
import com.book.entity.BookMember;
import com.book.entity.Teacher;
import com.book.utils.BeanUtils;
import com.book.utils.FTPTool;
import com.github.drinkjava2.jsqlbox.ActiveRecord;




/**
*
*      会员控制类
*
*/
public class MemberController  extends BaseController<BookMember>{ 
	
	@Autowired
	private FTPTool ftpTool;

	public MemberController() {
		super(); 
	}
	
	public ActiveRecord<BookMember>  getRecord() {
		return new BookMember();
	}
	
	public String  getPKFielName() {
		return "memberId";
	}
	
	@Override
	public String getModule() {
		return "member";
	}
	
	public String query(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		System.out.println("进入 UserController 的 query 方法。。。。");
		
		String username =  req.getParameter("username");
		String nicheng =  req.getParameter("nicheng");
		
		if(username != null && !"".equals(username.trim())) {
			req.setAttribute("datalist", getRecord().findAll(String.format(" where username like '%s'", "%"+ username +"%")));
		}else if(nicheng != null && !"".equals(nicheng .trim())) {
			req.setAttribute("datalist", getRecord().findAll(String.format(" where nicheng like '%s'", "%"+ nicheng +"%")));
		}else {
			req.setAttribute("datalist", getRecord().findAll());
		}
		return String.format("/%s/list",getModule() );

	}
	
	
	@Override
		public String save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		
		BookMember eo = (BookMember) getRecord();
		
		// 创建文件上传核心类
				ServletFileUpload sfu = new ServletFileUpload(new DiskFileItemFactory());
				sfu.setHeaderEncoding("UTF-8");// 设置编码
		// 处理表单请求
			    List<FileItem> itemList = sfu.parseRequest(req);
				
		for(FileItem item : itemList) {
			String fieldName = item.getFieldName();
			if(item.isFormField()) {
				//如果是普通表单文本数据
				String value = item.getString();
				BeanUtils.setProperty(eo, fieldName, new String(value.getBytes("ISO-8859-1"),"UTF-8")); 
				System.out.println(String.format("Name = %s; Value = %s", fieldName,value));
			}else {
				String imgUrl = "/" + System.nanoTime() + ".jpg";
				try {
					ftpTool.connect();
					ftpTool.uploadFileFromIS(imgUrl, item.getInputStream());
				
					BeanUtils.setProperty(eo, "imgUrl", imgUrl); 
					System.out.println(String.format("Name = %s; Value = %s", "imgUrl",imgUrl));
				
				
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		}		
			
		
		System.out.println(eo.toString());
		
		Object pkvalue =  eo.getField(getPKFielName());
		
		
		try {
		  //判断当前是新增 还是修改   
		if(pkvalue != null && ((Integer)pkvalue)>0) {
				//修改
				eo.update();
		}else {
			//新增
			eo.insert();
		   }		 
		}catch (Exception e) {
			e.printStackTrace();
			if(e.getMessage().contains("Duplicate entry")) {
				//有条目重复
				String msg = e.getMessage();
				int beginIndex = msg.indexOf("for key")+7;
				int endIndex = msg.indexOf("Query:");
				String key = msg.substring(beginIndex,endIndex);
				return String.format("%s重复，请重新填写", key);
			}else {
				return "更新失败，请联系管理员检查日志";
			}
			
			
		}	
				return "更新成功！";
	}
}
	

